import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false
      }
    ]
  },
  mutations: {
    // 数量++
    addCount (state, payload) {
      state.goodList[payload].num++
    },
    // 数量
    subCount (state, payload) {
      state.goodList[payload].num--
      if (state.goodList[payload].num <= 1) {
        state.goodList[payload].num = 1
      }
    },
    // 删除
    delData (state, payload) {
      state.goodList.splice(payload, 1)
    },
    // 添加
    inputAdd (state, payload) {
      state.goodList.push({
        name: payload.name,
        price: payload.price,
        num: payload.num,
        checked: payload.checked

      })
    },
    // 修改
    editInput (state, payload) {
      // console.log(payload)
      state.goodList.forEach(item => { item.checked = !payload })
    },
    editInputSamll (state, payload) {
      // console.log(payload)
      state.goodList[payload].checked = !state.goodList[payload].checked
    }

  },
  actions: {

  },
  getters: {
    count (state) {
      let sum = 0
      state.goodList.forEach(item => {
        if (item.checked === true) {
          sum += item.num * item.price
        }
      })
      return sum
    }

  },
  modules: {}
})
